package com.sanjiahantang.youjiaclient.module.common.adapters;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.module.evaluate.beans.GoodsAppraiseInitResponse;
import com.sanjiahantang.youjiaclient.module.goods.beans.GoodsBean;
import com.sanjiahantang.youjiaclient.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.sanjiahantang.youjiaclient.config.StaticValue.CALLBACK_TYPE_ADD_PICTURE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.CALLBACK_TYPE_DELETE_PICTURE;

/**
 * Created by zy on 2017/10/26.
 */

public class ImageAddAdapter extends BaseAdapter {

    private List<GoodsBean.Image> data;
    private Context context;
    private LayoutInflater inflater;
    private BaseView.OnLoadListener onLoadListener;
    private GoodsAppraiseInitResponse.Data bean;
    private int parentIndex;

    public ImageAddAdapter(Context context, Object[] param){
        this.data = new ArrayList<>();
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        bean = (GoodsAppraiseInitResponse.Data) param[0];
        BaseView.OnLoadListener listener = (BaseView.OnLoadListener) param[1];
        parentIndex = (int) param[2];
        this.onLoadListener = listener;
    }

    @Override
    public int getCount() {
        return data == null ? 1 : (data.size() + 1);
    }

    @Override
    public Object getItem(int i) {
        return i == getCount() - 1 ? null : data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (position < data.size()){
            convertView = inflater.inflate(R.layout.item_image, null);
        }else{
            convertView = inflater.inflate(R.layout.item_add_image, null);
        }

        holder = new ViewHolder(convertView);
        holder.bind(getItem(position));
        return convertView;
    }

    private class ViewHolder{
        private View itemView;
        public ViewHolder(View itemView){
            this.itemView = itemView;
        }

        public void bind(Object obj) {
            if (obj instanceof GoodsBean.Image){
                final GoodsBean.Image img = (GoodsBean.Image) obj;
                ImageView iv = (ImageView) itemView.findViewById(R.id.image);
                Picasso.with(context).load(FileUtils.rePath(img.getUrl())).into(iv);
                iv.setTag(R.id.decode,img);
                View v = itemView.findViewById(R.id.ivClose);
                v.setVisibility(View.VISIBLE);
                v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Object[] param = new Object[2];
                        param[0] = CALLBACK_TYPE_DELETE_PICTURE;
                        param[1] = img;
                        onLoadListener.onLoad(param);
                    }
                });

                iv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Utils.showPicDialog((Activity) context,FileUtils.rePath(img.getUrl()),R.drawable.i_rect_bg);
                    }
                });

            }else {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Object[] param = new Object[3];
                        param[0] = CALLBACK_TYPE_ADD_PICTURE;
                        param[1] = bean;
                        param[2] = parentIndex;
                        onLoadListener.onLoad(param);
                    }
                });
            }

        }
    }

    public void addAll(Collection<? extends GoodsBean.Image> list){
        if (list != null){
            data.addAll(list);
            notifyDataSetChanged();
        }

    }

    public void add(GoodsBean.Image url){
        data.add(url);
        notifyDataSetChanged();
    }

    public void clear(){
        data.clear();
        notifyDataSetChanged();
    }
}
